python's `with` statement

Posted by Prestel Nué on Stack Overflow See other posts from Stack Overflow or by Prestel Nué
Published on 2011-01-29T06:58:46Z Indexed on 2011/01/29 7:26 UTC
Read the original article Hit count: 130

Filed under:
|
|

Hi there,

seems like I do not understand something with---the python with statement.

Consider this class:

class test(object):
    def __enter__(self): pass
    def __exit__(self, *ignored): pass

now, when using it with with, like in

with test() as michael:
    print repr(michael)

I would expect some output like <test instance at memore blah>. But I get None.

Something wrong here? Any suggestions would help.

(I am using Python 2.6.6.)

EDIT:

Thanks to ephement for pointing me to the documentation. The __enter__ method should read

    def __enter__(self): return self

© Stack Overflow or respective owner

Related posts about python

Related posts about with-statement